package main

import "fmt"

type IFilter interface {
	Next() IFilter
	SetNext(next IFilter)
	Entry()
}

type fakeFilter struct {
	next  IFilter
	value int
}

func (f *fakeFilter) Next() IFilter {
	return f.next
}
func (f *fakeFilter) SetNext(next IFilter) {
	f.next = next
}
func (f *fakeFilter) Entry() {
	fmt.Println("-----", f.value)
	if f.next != nil {
		f.next.Entry()
	}
}

type FilterChain struct {
	head IFilter
}

func (chain *FilterChain) ExecuteFilterChain() {
	header := chain.head
	if header == nil {
		return
	}
	header.Entry()
}

func (chain *FilterChain) Append(filter IFilter) {
	if chain.head == nil {
		chain.head = filter
		return
	}
	cur := chain.head

	for cur.Next() != nil {
		cur = cur.Next()
	}
	cur.SetNext(filter)
}

func main() {
	filterChain := &FilterChain{}

	filterChain.Append(&fakeFilter{value: 1})
	filterChain.Append(&fakeFilter{value: 2})
	filterChain.Append(&fakeFilter{value: 3})

	filterChain.ExecuteFilterChain()

}
